package com.szbber.commen.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.launcher.ARouter

abstract class BaseFragment: Fragment() {

    private var contentView: View? = null
    private var mIsVisible = false // fragment是否显示了
    private val isPrepared = false
    private val isFirst = true //只加载一次界面

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        state: Bundle?
    ): View? {
        return if(contentView==null) {
            contentView = inflater.inflate(this@BaseFragment.getLayoutResId(), container, false)
            contentView
        }else {
            contentView
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(savedInstanceState)
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (userVisibleHint) { //fragment可见
            mIsVisible = true
            onVisible()
        } else { //fragment不可见
//            mIsVisible = false
//            attachView()
        }
    }

    protected open fun onVisible() {
        if (isFirst) {
        }
//        loadBaseData() //根据获取的数据来调用showView()切换界面
    }

    open fun loadBaseData() {
        if (!mIsVisible || !isPrepared || !isFirst) {
            return
        }
    }

    abstract fun getLayoutResId(): Int

    protected open fun initView(bundle: Bundle?) {}

    open fun getSupportActivity(): FragmentActivity? {
        return super.getActivity()
    }

    open fun getContentView(): View? {
        return contentView
    }



    //Arouter路由跳转封装， action代表Postcard方法体回调
    fun open(path: String) {
        val postcard = ARouter.getInstance().build(path)
        postcard.navigation()
    }





}